package com.fastCms.business.frame.service.impl;

import cn.hutool.core.io.FileUtil;
import com.fastCms.business.frame.enums.OssDirEnum;
import com.fastCms.business.frame.plugins.oss.OssTencentService;
import com.fastCms.business.frame.service.FileUploadManageService;
import com.fastCms.business.frame.utils.CommonsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Component
@Slf4j
public class FileUploadManageServiceImpl implements FileUploadManageService {

    @Autowired
    private OssTencentService ossTencentServiceImpl;


    @Value("${tencent.oss.bucket}")
    private String bucket;

    @Override
    public String uploadFile(MultipartFile file, String dirCode) throws IOException {
        String dirName = OssDirEnum.getEnum(dirCode).getName();
        String fileName = dirName + CommonsUtil.getNewName() + "." + FileUtil.getSuffix(file.getOriginalFilename());
        String storePath = null;
        try {
            storePath = ossTencentServiceImpl.uploadFile(bucket, fileName, file);
        } catch (Exception e) {
            log.error("[oss文件上传]--------error-------" + e.getMessage(), e);
            return null;
        }
        return storePath;
    }
}
